package cn.wolfcode.luowowo.mgrsite.web.Listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisticsService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisticsService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.common.key.RedisKey;
import com.alibaba.dubbo.config.annotation.Reference;
import javafx.application.Application;
import org.omg.CORBA.portable.ValueOutputStream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisticsService strategyStatisticsService;

    @Reference
    private ITravelStatisticsService travelStatisticsService;

    @Reference
    private ITravelService travelService;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        System.out.println("-------------------------------------初始化redis开始-------------------------------------");

        //先查询数据库  取出所有数据
        List<StrategyDetail> strategies = strategyDetailService.list();

        List<Travel> travels = travelService.list();

        //遍历数据,取出ID
        //初始化功略统计数据
        for (StrategyDetail strategyDetail : strategies) {

            String key = RedisKey.STRATEGY_STATIS_VO.appendKey(strategyDetail.getId().toString());

            if (strategyStatisticsService.hasKey(key)) {

                //存在就不初始化了
                continue;
            } else {

                StrategyStatisVO vo = new StrategyStatisVO();

                BeanUtils.copyProperties(strategyDetail, vo);

                //补充没有的参数
                if (strategyDetail.getDest() != null) {
                    vo.setDestName(strategyDetail.getDest().getName());
                    vo.setDestId(strategyDetail.getDest().getId());
                }

                vo.setStrategyId(strategyDetail.getId());

                //将vo存入
                strategyStatisticsService.setStrategyStatisVO(key,vo);
            }
        }

        //初始化游记统计数据
        for (Travel travel : travels) {

            String key = RedisKey.TRAVEL_STATIS_VO.appendKey(travel.getId().toString());

            if(travelStatisticsService.hasKey(key)){

                continue;
            }else{

                TravelStatisVO t_vo = new TravelStatisVO();

                BeanUtils.copyProperties(travel,t_vo);

                //补充没有的数据
                if (travel.getDest() != null) {
                    t_vo.setDestName(travel.getDest().getName());
                    t_vo.setDestId(travel.getDest().getId());
                }

                t_vo.setTravelId(travel.getId());

                //将vo存入
                travelStatisticsService.setTravelStatisVO(key,t_vo);

            }


        }

        System.out.println("-------------------------------------初始化redis统计数据结束-------------------------------------");

        System.out.println("-------------------------------------初始化redis推荐排行数据开始-------------------------------------");

        //初始化国内外的攻略推荐排行数据
        for (StrategyDetail strategy : strategies) {

            String key = RedisKey.STRATEGY_STATIS_ISABROAD_SORT.getPrefix();

            String name = RedisKey.STRATEGY_STATIS_VO.appendKey(strategy.getId().toString());

            //判断这个name在key中是否存在  不存在才进行初始化
            if(strategyStatisticsService.isExistInZset(key,name)){

                continue;

            }else{

                //初始化参数  初始的值收藏数 + 点赞数
                strategyStatisticsService.addScore(key,name,strategy.getFavornum() + strategy.getThumbsupnum());

            }
        }
        String key = RedisKey.STRATEGY_STATIS_HOT_SORT.getPrefix();

        //初始化热门的攻略推荐排行数据
        for (StrategyDetail strategy : strategies) {

            String name = RedisKey.STRATEGY_STATIS_VO.appendKey(strategy.getId().toString());

            if(strategyStatisticsService.isExistInZset(key,name)){

                continue;
            }else{

                strategyStatisticsService.addScore(key,name,strategy.getViewnum() + strategy.getReplynum());
            }

        }

        System.out.println("-------------------------------------初始化redis推荐排行数据结束-------------------------------------");
    }

}
